/*
 * @Author: dingyuwen ding_yuwen@163.com
 * @Date: 2023-01-09 17:18:10
 * @LastEditTime: 2023-07-31 23:43:51
 * @LastEditors: DingYuwen 43669762+DingYuwen@users.noreply.github.com
 * @Description:
 */
import type { PayloadAction } from '@reduxjs/toolkit';
import { createSlice } from '@reduxjs/toolkit';
export declare type Image = {
  uuid: string;
  customer_img_id: string;
  isCollect: number;
  url: string;
  thumbnail: string;
  mtime: number;
  metadata?: any;
  width: number;
  height: number;
  category: GalleryCategory;
  isBase64?: boolean;
  dreamPrompt?: 'string';
};
export interface RecommendInfo {
  code: string;
  commissionPayed: number;
  commissionPaying: number;
  commissionResidual: number;
  countInvitee: number;
  [k: string]: any;
}
export type GalleryCategory = 'user' | 'result';

export type AddImagesPayload = {
  images: Array<Image>;
  areMoreImagesAvailable: boolean;
  category: GalleryCategory;
  current_page: number;
};

export type Gallery = {
  images: Image[];
  latest_mtime?: number;
  earliest_mtime?: number;
  areMoreImagesAvailable: boolean;
  current_page?: number;
  inviteeList?: any[];
};

interface ProfileState {
  isProfileGetImage: boolean;
  isProfileGetImageLoading: boolean;
  recommendInfo: RecommendInfo;
  commissionRecordList?: {
    [k: string]: any;
  }[];
  gallery: Gallery;
}

const initialState: ProfileState = {
  isProfileGetImage: false,
  isProfileGetImageLoading: true,
  recommendInfo: {
    code: '',
    commissionPayed: 0,
    commissionPaying: 0,
    commissionResidual: 0,
    countInvitee: 0,
    inviteeList: [],
  },
  gallery: {
    images: [],
    areMoreImagesAvailable: true,
  },
};

export const profileSlice = createSlice({
  name: 'profile',
  initialState,
  reducers: {
    setRecommendInfo: (state, { payload }) => {
      state.recommendInfo = { ...state.recommendInfo, ...payload };
    },
    setCommissionRecordList: (state, { payload }) => {
      state.commissionRecordList = payload;
    },
    setIsProfileGetImage: (state, { payload }) => {
      state.isProfileGetImage = payload;
    },
    setIsProfileGetImageLoading: (state, { payload }) => {
      state.isProfileGetImageLoading = payload;
    },
    addGalleryImages: (state, action: PayloadAction<AddImagesPayload>) => {
      const { images, areMoreImagesAvailable, category, current_page } =
        action.payload;
      if (category === 'user') {
        return;
      }

      const tempImages = state.gallery.images;
      if (images.length > 0) {
        const newImages = images.filter(
          (newImage) =>
            !tempImages.find(
              (i) => i.url === newImage.url && i.mtime === newImage.mtime
            )
        );
        state.gallery.images = state.gallery.images
          .concat(newImages)
          .sort((a, b) => b.mtime - a.mtime);

        state.gallery.current_page = current_page;
      }

      if (areMoreImagesAvailable !== undefined) {
        state.gallery.areMoreImagesAvailable = areMoreImagesAvailable;
      }
    },
    reset: (state) => {
      state.gallery = {
        images: [],
        areMoreImagesAvailable: true,
      };
      state.isProfileGetImage = false;
      state.recommendInfo = {
        code: '',
        commissionPayed: 0,
        commissionPaying: 0,
        commissionResidual: 0,
        countInvitee: 0,
        inviteeList: [],
      };
    },
  },
});

export const {
  setRecommendInfo,
  setCommissionRecordList,
  setIsProfileGetImage,
  addGalleryImages,
  reset,
  setIsProfileGetImageLoading,
} = profileSlice.actions;

export default profileSlice.reducer;
